import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'common/constants.dart';
import 'controllers/home_controller.dart';

class HomePage extends GetView<HomeController> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => PopScope(
        canPop: controller.backPressAllowed.value,
        onPopInvokedWithResult: (didPop, result) async {
          if (!didPop) {
            controller.allowBackPress();
            Get.snackbar(
              '',
              '',
              backgroundColor: Colors.transparent,
              snackPosition: SnackPosition.TOP,
              duration: const Duration(milliseconds: 1500),
              titleText: SizedBox.shrink(),
              messageText: Center(
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/android12splash.png",
                      width: 50,
                      height: 50,
                    ),
                    Text("再按一次退出应用", style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: Colors.black))
                  ],
                ),
              ),
            );
          }
        },
        child: Scaffold(
          body: IndexedStack(
            index: controller.selectedIndex.value,
            children: controller.pages,
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(icon: SizedBox.shrink(), label: '首页'),
              BottomNavigationBarItem(icon: SizedBox.shrink(), label: '角色'),
              BottomNavigationBarItem(icon: SizedBox.shrink(), label: '消息'),
              BottomNavigationBarItem(icon: SizedBox.shrink(), label: '视频'),
              BottomNavigationBarItem(icon: SizedBox.shrink(), label: '我的'),
            ],
            selectedItemColor: Constants.themeColor,
            unselectedItemColor: Colors.grey,
            selectedLabelStyle: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
            unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.normal, fontSize: 15),
            type: BottomNavigationBarType.fixed,
            currentIndex: controller.selectedIndex.value,
            onTap: (index) => controller.onItemTapped(index, context),
          ),
        ),
      ),
    );
  }
}
